package kr.co.hk;


import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.ArrayList;
import kr.co.ks.FacDetails;
import kr.co.ks.FacInfo;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.Toast;

public class Detail extends Activity {
	
	ListView listView;
	ArrayList<FacInfo> arrayList;
	ArrayAdapter<FacInfo> arrayAdapter;
	ArrayList seqList;

	String totCnt = "";
	String pageIndex = "";
	String group = "";
	String facName = "";
	String seq = "";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main3);
		
		ListView listView = (ListView) findViewById(R.id.listView1);
	
	
		
		arrayList = new ArrayList<FacInfo>();
		seqList = new ArrayList();

		arrayAdapter = new ArrayAdapter<FacInfo>(this,
				android.R.layout.simple_list_item_1, arrayList);
		listView.setAdapter(arrayAdapter);
		listView.setOnItemClickListener(itemclick);
		
		Bundle extra = getIntent().getExtras();
		int bun = extra.getInt("bun");
		int no = extra.getInt("no");
		
	

		Resources r = getResources();
		int[] fac_code = r.getIntArray(R.array.searchFacCl_code);
		String[] local = r.getStringArray(R.array.searchLocal);
		
		int num = no +1;
		

		String searchFacCl = fac_code[bun+1] + "";
		String searchLocal = local[no] + "";
		
		if (searchLocal.equals("��ü"))
			searchLocal = "";
		if (searchFacCl.equals("0"))
			searchFacCl = "";


		String encodedLocal = "";
		String encodedKeyword = "";
		try {
			encodedLocal = URLEncoder.encode(searchLocal, "euc-kr");
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}

		System.out.println(searchFacCl);
		System.out.println(encodedLocal);
		String html;
		html = "http://www.women.go.kr/new_women/women/info/fac/selectFacList.do?"

				+ "searchFacCl="
				+ searchFacCl
				+ "&searchKeyword="
				+ encodedKeyword

				+ "&searchLocal=" + encodedLocal;

		getFacList(html);
	}
// ��
	public void  getFacList(String addr) {
		try {
			URL url = new URL(addr);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			if (conn != null) {
				conn.setConnectTimeout(10000);
				conn.setUseCaches(false);
				if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
					BufferedReader br = new BufferedReader(
							new InputStreamReader(conn.getInputStream(),
									"euc-kr"));
					ArrayList<String> list = new ArrayList<String>();
					String msg = "";
					while ((msg = br.readLine()) != null) {
						list.add(msg.trim() + "\n");
					}

					listClear();
					for (int i = 0; i < list.size(); i++) {

						boolean isChk = false;
						String line = list.get(i);

						String reStr = line;
						int firstIndex = 0;
						int lastIndex = 0;

						// totcnt
						if (line.contains("id=\"totCnt")) {
							firstIndex = reStr.indexOf("value=");
							lastIndex = reStr.indexOf("\" />");
							totCnt = reStr.substring(firstIndex + 7, lastIndex);
						}
						// pageIndex
						if (line.contains("id=\"pageIndex")) {
							firstIndex = reStr.indexOf("value=");
							lastIndex = reStr.indexOf("\"/>");
							pageIndex = reStr.substring(firstIndex + 7, lastIndex);
						}
						if (line.contains("class=\"author")) {

							firstIndex = reStr.indexOf("\">");
							lastIndex = reStr.indexOf("</td>");
							group = reStr.substring(firstIndex + 2, lastIndex);
						}

						if (line.contains("javascript:fncGoView")) {

							firstIndex = reStr.indexOf(";\">");
							lastIndex = reStr.indexOf("</a></td>");
							facName = reStr.substring(firstIndex + 3, lastIndex);
							int fIndex = reStr.indexOf("(");
							int lIndex = reStr.indexOf(");");
							seq = reStr.substring(fIndex + 1, lIndex);
						}

						if (!group.equals("") && !facName.equals("") && !seq.equals("")) {
							isChk = true;

						}

						if (isChk) {
							FacInfo facinfo = new FacInfo(facName, group, seq);
							addListToArray(facinfo);
							seq = "";
							group = "";
							facName = "";
						}

					}
					CharSequence text = "��ü�Ǽ� : " + totCnt;
					Toast.makeText(this, text, Toast.LENGTH_SHORT).show();

					br.close();
				}
				conn.disconnect();
			}
		} catch (Exception ex) {
			Toast.makeText(this, ex.getMessage(), Toast.LENGTH_SHORT).show();
		}
	}

	// itemclick
	AdapterView.OnItemClickListener itemclick = new AdapterView.OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View v, int position,
				long id) {
			CharSequence text = "seq : " + seqList.get(position);
			String strSeq = (String) seqList.get(position);
			toast(text, Toast.LENGTH_SHORT);
			Intent intent = new Intent(Detail.this, FacDetails.class);
			intent.putExtra("seq", strSeq);
			startActivity(intent);

		}

	};

	private void toast(CharSequence text, int duration) {
		Toast.makeText(this, text, duration).show();
	}

	// listview �߰�
	private void addListToArray(FacInfo facinfo) {
		arrayList.add(facinfo);
		seqList.add(facinfo.getSeq());
		arrayAdapter.notifyDataSetChanged();
	}

	private void listClear() {
		arrayList.clear();
		seqList.clear();
		arrayAdapter.clear();
	}

}
